package net.spatula.tally_ho.utils.test;

/* Copyright 2006 Nicklas R Johnson
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ObjectDumper {

	public static void dump(Object o) {
		Class objectClass = o.getClass();
		Field[] fields = objectClass.getFields();
		Method[] methods = objectClass.getMethods();
		System.out.println("Dump of " + objectClass.getCanonicalName()
				+ " with hashcode " + o.hashCode());
		for (Field field : fields) {
			
			if (Modifier.isPublic(field.getModifiers())) {
				try {
					field.setAccessible(true);
					System.out.print(field.getName() + "=[");
					System.out.print(field.get(o));
				} catch (IllegalArgumentException e) {
					System.out.println("(illegal argument)");
				} catch (IllegalAccessException e) {

				}
				System.out.println("]\n");
			}
		}
		for (Method method : methods) {
		
			if (method.getName().matches("^(is|get).*") && Modifier.isPublic(method.getModifiers())&& (method.getParameterTypes()).length == 0) {

				System.out.println(method.getName() + "=[");
				try {
					method.setAccessible(true);
					System.out.println(method.invoke(o, null));
				} catch (IllegalArgumentException e) {
					System.out.println("(illegal argument)");
				} catch (IllegalAccessException e) {
					System.out.println("(illegal access)");
				} catch (InvocationTargetException e) {
					System.out.println("(bad invocation)");
				}
				System.out.println("]\n");

			}
		}
	}

}
